package com.example.mysecondprojecct.common;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @description: 返回请求统一处理
* @author MT
* @date 2023/10/11 21:56
* @version 1.0
*/


public class Result <T>{

    private Integer code;
    private String message;

    private T data;

    public Result(Integer code,String message){
        this.code=code;
        this.message=message;
    }
    public Result(){

    }

    public Result(T data){this.data=data;}

    public static Result success(){
        return  new Result<>(200, "成功");
    }
    public static <T> Result<T> success(T data){
        Result result = new Result<T>(data);
        result.setCode(200);
        result.setMessage("成功");
        return result;
    }
    public static <T> Result<T> success(T data , String message){
        Result result = new Result<T>(data);
        result.setCode(200);
        result.setMessage(message);
        return result;
    }

    public static Result faile(String message){
        Result result=new Result<>();

        result.setCode(1);
        result.setMessage(message);
        return result;
    }
    public static Result faile(Integer code ,String message){
        Result result=new Result<>();

        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static Result error(String message){
        Result result=new Result<>();

        result.setCode(-1);
        result.setMessage(message);
        return result;
    }

    public static Result faile(Integer key,String message, Date date){

        Result result=new Result<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        result.setCode(key);
        result.setMessage(message+sdf.format(date));
        return result;
    }



    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message){
        this.message=message;
    }


    public T getData() {return data;}

    public void setData(T data) {this.data = data;}
}
